package cn.demomaster.quickrouter.lifecycle;

import android.app.Activity;

import androidx.fragment.app.Fragment;

import java.util.ArrayList;
import java.util.List;

/**
 * 生命周期轨道
 */
public class LifecycleTrace {
    private List<LifecycleEvent> lifecycleEvents;
    private long createTime;
    private long destroyTime;
    private long lastTime;
    private boolean isDestroyed;
    private String traceName;
    private int traceHash;
    private boolean isActivityTrace;
    private boolean  isFragmentTrace;

    public LifecycleTrace(Activity activity) {
        this.traceName = activity.getClass().getName();
        this.traceHash = activity.hashCode();
        this.createTime = System.currentTimeMillis();
        this.lifecycleEvents = new ArrayList<>();
        this.isActivityTrace = true;
    }

    public LifecycleTrace(Fragment fragment) {
        this.traceName = fragment.getClass().getName();
        this.traceHash = fragment.getActivity().hashCode();
        this.createTime = System.currentTimeMillis();
        this.lifecycleEvents = new ArrayList<>();
        this.isFragmentTrace = true;
    }

    public List<LifecycleEvent> getLifecycleEvents() {
        return lifecycleEvents;
    }

    public void setLifecycleEvents(List<LifecycleEvent> lifecycleEvents) {
        this.lifecycleEvents = lifecycleEvents;
    }

    public long getCreateTime() {
        return createTime;
    }

    public void setCreateTime(long createTime) {
        this.createTime = createTime;
    }

    public long getDestroyTime() {
        return destroyTime;
    }

    public void setDestroyTime(long destroyTime) {
        this.destroyTime = destroyTime;
    }

    public boolean isDestroyed() {
        return isDestroyed;
    }

    public void setDestroyed(boolean destroyed) {
        isDestroyed = destroyed;
        destroyTime  = System.currentTimeMillis();
    }

    public long getLastTime() {
        return lastTime;
    }

    public void setLastTime(long lastTime) {
        this.lastTime = lastTime;
    }

    public String getTraceName() {
        return traceName;
    }

    public void setTraceName(String traceName) {
        this.traceName = traceName;
    }

    public int getTraceHash() {
        return traceHash;
    }

    public void setTraceHash(int traceHash) {
        this.traceHash = traceHash;
    }

    public boolean isActivityTrace() {
        return isActivityTrace;
    }

    public void setActivityTrace(boolean activityTrace) {
        isActivityTrace = activityTrace;
    }

    public boolean isFragmentTrace() {
        return isFragmentTrace;
    }

    public void setFragmentTrace(boolean fragmentTrace) {
        isFragmentTrace = fragmentTrace;
    }

    public void addLifecycleEvent(LifecycleStateType lifecycleStateType) {
        LifecycleEvent lifecycleEvent = new LifecycleEvent();
        lifecycleEvent.setLifecycleType(lifecycleStateType);
        lifecycleEvent.setCreateTime(System.currentTimeMillis());
        lifecycleEvents.add(lifecycleEvent);
        lastTime = lifecycleEvent.getCreateTime();
    }

}
